package com.sofoscorp.android.initdb;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
    
	private ToggleButton btnCreateLO;
	private ToggleButton btnCreateFI;
	private ToggleButton btnCreateQM;
	
	//Preferencias
	private int gv_versionFromSAP;
	private int gv_versionToSAP;
	
	//Objetos de Logistica
	private DBHelperFromSAP_LO go_db_fromSAP_lo;
	private DBHelperToSAP_LO go_db_toSAP_lo;
	
	//Objetos Finanzas
	private DBHelperFromSAP_FI go_db_fromSAP_fi;
	private DBHelperToSAP_FI go_db_toSAP_fi;
	
	private Toast go_toast;
	private SQLiteDatabase go_db_fromSAP;
	private SQLiteDatabase go_db_toSAP;

	//Directorio DB
	private String gv_path_FromSAP = "data/data/com.sofoscorp.android.initdb/databases/FromSAP";
	private String gv_path_ToSAP   = "data/data/com.sofoscorp.android.initdb/databases/ToSAP";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//btnCreateDB = (Button)findViewById(R.id.btnCreateDB);
		
		btnCreateFI = (ToggleButton)findViewById(R.id.btnCreateFI);
		btnCreateLO = (ToggleButton)findViewById(R.id.btnCreateLO);
		btnCreateQM = (ToggleButton)findViewById(R.id.btnCreateQM);
		
		//Crear tablas logisticas
		btnCreateLO.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (btnCreateLO.isChecked()) {
						gv_versionFromSAP = 0;
						openDB();
						gv_versionFromSAP = gv_versionFromSAP + 1;
						//Crear BDs y Tablas Logistica
						go_db_fromSAP_lo = new DBHelperFromSAP_LO(getApplicationContext(), "FromSAP", null, gv_versionFromSAP);
						go_db_toSAP_lo   = new DBHelperToSAP_LO(getApplicationContext(), "ToSAP", null, gv_versionFromSAP);
							
						//Activar modo Lectura/Escritura
						go_db_fromSAP = go_db_fromSAP_lo.getWritableDatabase();
						go_db_toSAP   = go_db_toSAP_lo.getWritableDatabase();
							
						if (go_db_fromSAP != null && go_db_toSAP != null){
							go_toast = Toast.makeText(getApplicationContext(), R.string.dbs_created_lo, Toast.LENGTH_SHORT);
							go_toast.show();					
						}else{	
						}
					}
				}
		});
		
		//Crear tablas Financieras
		btnCreateFI.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (btnCreateFI.isChecked()) {
						gv_versionFromSAP = 0;
						openDB();
						gv_versionFromSAP = gv_versionFromSAP + 1; 
						//Crear BDs y Tablas Logistica
						go_db_fromSAP_fi = new DBHelperFromSAP_FI(getApplicationContext(), "FromSAP", null, gv_versionFromSAP);
						go_db_toSAP_fi   = new DBHelperToSAP_FI(getApplicationContext(), "ToSAP", null, gv_versionFromSAP);
							
						//Activar modo Lectura/Escritura
						go_db_fromSAP = go_db_fromSAP_fi.getWritableDatabase();
						go_db_toSAP   = go_db_toSAP_fi.getWritableDatabase();
							
						if (go_db_fromSAP != null && go_db_toSAP != null){
							go_toast = Toast.makeText(getApplicationContext(), R.string.dbs_created_fi, Toast.LENGTH_SHORT);
							go_toast.show();					
						}else{	
						}
					}
				}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()){
		case R.id.menu_settings:
			//llamar Pantalla de Preferencias
			Intent intent = new Intent(MainActivity.this, OptionsActivity.class);
			startActivity(intent);
		default:
			return super.onOptionsItemSelected(item);			
		}
	}
	
	/* *
	 * Aperturar base de datos
	 * */ 
	public void openDB(){
		//Aperturar DB fromSAP en modo escritura y lectura
		try{
			go_db_fromSAP = SQLiteDatabase.openDatabase(gv_path_FromSAP, null, 
													 	SQLiteDatabase.OPEN_READWRITE);
			gv_versionFromSAP = go_db_fromSAP.getVersion();
		}catch(SQLiteException e){
			
		}		
		
		//Aperturar DB toSAP en modo escritura y lectura
		try{
			go_db_toSAP = SQLiteDatabase.openDatabase(gv_path_ToSAP, null, 
												   	  SQLiteDatabase.OPEN_READWRITE);
			gv_versionToSAP = go_db_fromSAP.getVersion();
		}catch(SQLiteException e){
			
		}
		
		//Verificar si DB ya existe o no
		if (go_db_fromSAP != null & go_db_toSAP != null){
			go_toast = Toast.makeText(getApplicationContext(), R.string.dbs_exists, Toast.LENGTH_SHORT);
		}else{
			go_toast = Toast.makeText(getApplicationContext(), R.string.dbs_not_exist, Toast.LENGTH_SHORT);
		}
		go_toast.show();
	}
}
